package de.fhkoeln.avs.reader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class TestfileDateReaderImpl implements DataReader{
	
	private BufferedReader reader;
	private String stopValue;
	private String delim;
	
	public TestfileDateReaderImpl(File file, String stopValue, String delim) throws FileNotFoundException{
		this.stopValue = stopValue;
		this.delim = delim;
		reader = new BufferedReader(new FileReader(file));
	}
	
	@Override
	public List<String> getNext() throws IOException{
		String input = loadFromFile();
		if(input == null)
			return null;
		
		return buildWordAndSynonymList(input);
	}
	
	private List<String> buildWordAndSynonymList(String input) {
		StringTokenizer tokenizer = new StringTokenizer(input, delim);	
		List<String> wordAndSynonyms = new ArrayList<String>();	
		while(tokenizer.hasMoreTokens()){
			String wordOrSynonym = tokenizer.nextToken();
			wordOrSynonym = wordOrSynonym.toLowerCase().trim();
			if(!wordOrSynonym.contains(stopValue)){
				wordAndSynonyms.add(wordOrSynonym);
			}else{
				return wordAndSynonyms;
			}
		}
		if(!wordAndSynonyms.isEmpty())
			return wordAndSynonyms;
		return null;
	}

	private String loadFromFile() throws IOException {
		String input = "";
//		do{
			String newInput = reader.readLine();
			if(newInput == null){
				return null;
			}
			input = input + newInput;
			
//		}while(!input.contains(stopValue));
		return input;
	}

}
